﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using XCI.Core;
using XCI.Sys.Model;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统部门服务
    /// </summary>
    public class DeptService : BaseService<SysDept>
    {
        public static readonly DeptService Instance = new DeptService();
        protected override string RootUrl => "/api/sys/dept/";

        /// <summary>
        /// 新建部门模型
        /// </summary>
        public SysDept New()
        {
            return new SysDept
            {
                Id = NewId(),
                Status = true
            };
        }

        /// <summary>
        /// 复制部门模型
        /// </summary>
        /// <param name="original">原模型</param>
        public SysDept Copy(SysDept original)
        {
            if (original == null) throw new ArgumentNullException(nameof(original));
            var model = original?.Clone();
            model.Id = NewId();
            return model;
        }

        /// <summary>
        /// 检查部门主键是否存在
        /// </summary>
        /// <param name="id">部门主键</param>
        public bool ExistById(string id)
        {
            return ExistByIdCore(id);
        }

        /// <summary>
        /// 检查部门编码是否存在
        /// </summary>
        /// <param name="code">部门编码</param>
        public bool ExistByCode(string code)
        {
            return ExistByCodeCore(code);
        }

        /// <summary>
        /// 检查部门名称是否存在
        /// </summary>
        /// <param name="parentId">部门上级主键</param>
        /// <param name="name">部门名称</param>
        public bool ExistByName(string parentId, string name)
        {
            var url = GetUrl("existByName", parentId, name);
            return SysRuntime.Api.Post<bool>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 新建部门
        /// </summary>
        /// <param name="model">部门模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Insert(SysDept model)
        {
            return InsertCore(model);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model">部门模型对象</param>
        /// <returns>成功返回true</returns>
        public bool Update(SysDept model)
        {
            return UpdateCore(model);
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        /// <returns>成功返回true</returns>
        public bool UpdateStatus(string ids, bool status)
        {
            return UpdateStatusByIdsCore(ids, status);
        }

        /// <summary>
        /// 批量修改字段值
        /// </summary>
        /// <param name="keyValues">修改的数据集合</param>
        /// <returns>成功返回true</returns>
        public bool BatchUpdateValues(List<PrimaryKeyValue> keyValues)
        {
            return BatchUpdateValuesCore(keyValues);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public bool Delete(string ids)
        {
            return DeleteByIdsCore(ids);
        }

        /// <summary>
        /// 根据主键获取部门对象
        /// </summary>
        /// <param name="id">部门主键</param>
        public SysDept SelectById(string id)
        {
            return SelectByIdCore(id);
        }

        /// <summary>
        /// 根据部门编码获取部门对象
        /// </summary>
        /// <param name="code">部门编码</param>
        /// <returns>成功返回模型对象</returns>
        public SysDept SelectByCode(string code)
        {
            return SelectByCodeCore(code);
        }

        /// <summary>
        /// 获取节点的直系子节点数
        /// </summary>
        /// <param name="id">部门主键</param>
        public int SelectChildCount(string id)
        {
            var url = GetUrl("selectChildCount", id);
            return SysRuntime.Api.Post<int>(url).EnsureSuccess().Data;
        }

        /// <summary>
        /// 查询部门列表
        /// </summary>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public List<SysDept> SelectList(string status)
        {
            var map = Map.New.IsNotEmpty(SysConst.FStatusName, status);
            return SelectListCore(map);
        }

        /// <summary>
        /// 导出部门列表
        /// </summary>
        /// <param name="status">状态(0:禁用;1:启用;null:全部记录)</param>
        public byte[] Export(string status)
        {
            var map = Map.New.IsNotEmpty(SysConst.FStatusName, status);
            return ExportCore(map);
        }

        /// <summary>
        /// 刷新服务端部门缓存
        /// </summary>
        public bool RefreshCache()
        {
            return RefreshCacheCore();
        }
    }
}